<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  ~
  -->

<div class="sp-dialog-container">
    <div class="sp-dialog-content" *ngIf="usersLoaded">
        <div fxFlex="100" fxLayout="column" class="p-15">
            <h4>{{ headerTitle }}</h4>
            <form [formGroup]="parentForm" fxFlex="100" fxLayout="column">
                <div class="general-options-panel" fxLayout="column">
                    <span class="general-options-header">Basics</span>
                    <mat-form-field color="accent">
                        <mat-label>Owner</mat-label>
                        <mat-select formControlName="owner" fxFlex required>
                            <mat-option
                                *ngFor="let user of allUsers"
                                [value]="user.principalId"
                                >{{ user.username }}</mat-option
                            >
                        </mat-select>
                    </mat-form-field>
                    <mat-checkbox
                        data-cy="permission-public-element"
                        formControlName="publicElement"
                    >
                        Public Element
                    </mat-checkbox>
                </div>
                <div
                    fxLayout="column"
                    class="general-options-panel"
                    *ngIf="!permission.publicElement"
                >
                    <span class="general-options-header">Users</span>
                    <mat-form-field color="accent">
                        <mat-label>Authorized Users</mat-label>
                        <mat-chip-grid #chipList aria-label="User selection">
                            <mat-chip-row
                                *ngFor="let user of grantedUserAuthorities"
                                selectable="true"
                                removable="true"
                                (removed)="removeUser(user)"
                            >
                                {{ user.username }}
                                <button matChipRemove>
                                    <mat-icon>cancel</mat-icon>
                                </button>
                            </mat-chip-row>
                            <input
                                placeholder="Add"
                                #userInput
                                [formControl]="userCtrl"
                                [matAutocomplete]="auto"
                                [matChipInputFor]="chipList"
                                [matChipInputSeparatorKeyCodes]="
                                    separatorKeysCodes
                                "
                                data-cy="authorized-user"
                                (matChipInputTokenEnd)="addUser($event)"
                            />
                        </mat-chip-grid>
                        <mat-autocomplete
                            #auto="matAutocomplete"
                            (optionSelected)="userSelected($event)"
                        >
                            <mat-option
                                *ngFor="let user of filteredUsers | async"
                                [value]="user"
                                [attr.data-cy]="'user-option-' + user.username"
                            >
                                {{ user.username }}
                            </mat-option>
                        </mat-autocomplete>
                    </mat-form-field>
                </div>
                <div
                    fxLayout="column"
                    class="general-options-panel"
                    *ngIf="!permission.publicElement"
                >
                    <span class="general-options-header">Groups</span>
                    <mat-form-field color="accent">
                        <mat-label>Authorized Groups</mat-label>
                        <mat-chip-grid #chipList aria-label="Group selection">
                            <mat-chip-row
                                *ngFor="let group of grantedGroupAuthorities"
                                selectable="true"
                                removable="true"
                                (removed)="removeGroup(group)"
                            >
                                {{ group.groupName }}
                                <button matChipRemove>
                                    <mat-icon>cancel</mat-icon>
                                </button>
                            </mat-chip-row>
                            <input
                                placeholder="Add"
                                #groupInput
                                [formControl]="groupCtrl"
                                [matAutocomplete]="auto"
                                [matChipInputFor]="chipList"
                                [matChipInputSeparatorKeyCodes]="
                                    separatorKeysCodes
                                "
                                (matChipInputTokenEnd)="addGroup($event)"
                            />
                        </mat-chip-grid>
                        <mat-autocomplete
                            #auto="matAutocomplete"
                            (optionSelected)="groupSelected($event)"
                        >
                            <mat-option
                                *ngFor="let group of filteredGroups | async"
                                [value]="group"
                            >
                                {{ group.groupName }}
                            </mat-option>
                        </mat-autocomplete>
                    </mat-form-field>
                </div>
            </form>
        </div>
    </div>
    <mat-divider></mat-divider>
    <div class="sp-dialog-actions">
        <div fxLayout="row">
            <button
                mat-button
                mat-raised-button
                color="accent"
                (click)="save()"
                style="margin-right: 10px"
                [disabled]="!parentForm.valid"
                data-cy="sp-manage-permissions-save"
            >
                <i class="material-icons">save</i><span>&nbsp;Save</span>
            </button>
            <button
                mat-button
                mat-raised-button
                class="mat-basic"
                (click)="close(false)"
            >
                Cancel
            </button>
        </div>
    </div>
</div>
